Page({
  data: {
    selectedContent: 'A',  // 默认选中A内容
    selectedCapacity: 5,    // 默认选择5人局
    openid: '',             // 存储获取到的openid
    roomId: '',             // 房间号
    // 角色图片路径
    roleImages: {
        merlin: 'cloud://avalon-cloud-1gg01hi5d0e8cec0.6176-avalon-cloud-1gg01hi5d0e8cec0-1330536532/images/role/1.png',
        morgana: 'cloud://avalon-cloud-1gg01hi5d0e8cec0.6176-avalon-cloud-1gg01hi5d0e8cec0-1330536532/images/role/2.png',
        loyal: 'cloud://avalon-cloud-1gg01hi5d0e8cec0.6176-avalon-cloud-1gg01hi5d0e8cec0-1330536532/images/role/3.png',
        loyal2: 'cloud://avalon-cloud-1gg01hi5d0e8cec0.6176-avalon-cloud-1gg01hi5d0e8cec0-1330536532/images/role/4.png',
        loyal3: 'cloud://avalon-cloud-1gg01hi5d0e8cec0.6176-avalon-cloud-1gg01hi5d0e8cec0-1330536532/images/role/5.png',
        loyal4: 'cloud://avalon-cloud-1gg01hi5d0e8cec0.6176-avalon-cloud-1gg01hi5d0e8cec0-1330536532/images/role/6.png',
        assasin: 'cloud://avalon-cloud-1gg01hi5d0e8cec0.6176-avalon-cloud-1gg01hi5d0e8cec0-1330536532/images/role/7.png',
        modred: 'cloud://avalon-cloud-1gg01hi5d0e8cec0.6176-avalon-cloud-1gg01hi5d0e8cec0-1330536532/images/role/8.png',
        oberon: 'cloud://avalon-cloud-1gg01hi5d0e8cec0.6176-avalon-cloud-1gg01hi5d0e8cec0-1330536532/images/role/9.png',
        minion: 'cloud://avalon-cloud-1gg01hi5d0e8cec0.6176-avalon-cloud-1gg01hi5d0e8cec0-1330536532/images/role/10.png',
        percival: 'cloud://avalon-cloud-1gg01hi5d0e8cec0.6176-avalon-cloud-1gg01hi5d0e8cec0-1330536532/images/role/11.png',
      }
  },

  // 点击图片时，更新显示的内容
  selectContent(event) {
    const content = event.currentTarget.dataset.content;  // 获取当前点击的图片标识
    const capacity = event.currentTarget.dataset.capacity; // 获取选择的人数
    this.setData({
      selectedContent: content,  // 更新显示的内容
      selectedCapacity: capacity // 更新选中的人数
    });
  },

  // 点击创建房间的事件处理函数
  async createRoom() {
    try {
      const selectedCapacity = this.getSelectedCapacity();  // 获取选择的人数
      const openid = await this.getOpenid();
      const roomId = await this.createRoomInCloud(openid,selectedCapacity);
      
      this.setData({ roomId });
      wx.showToast({
        title: '房间创建成功',
        icon: 'success',
        duration: 2000
      });
  
      
  
      wx.navigateTo({
        url: `/pages/room/room?roomId=${roomId}&ownerOpenid=${openid}&selectedCapacity=${selectedCapacity}`,
        success: function(res) {
          console.log('跳转成功，roomId:', roomId, 'ownerOpenid:', openid);
        },
        fail: function(err) {
          console.error('跳转失败:', err);
        }
      });
    } catch (error) {
      console.error('创建房间失败', error);
      wx.showToast({
        title: '创建房间失败',
        icon: 'none',
        duration: 2000
      });
    }
  },
  
  getSelectedCapacity() {
    switch (this.data.selectedContent) {
      case 'A': return 5;
      case 'B': return 6;
      case 'C': return 7;
      case 'D': return 8;
      case 'E': return 9;
      case 'F': return 10;
      default: return 5;
    }
  }
  ,
  

  // 获取用户openid的函数
  getOpenid() {
    return new Promise((resolve, reject) => {
      wx.cloud.callFunction({
        name: 'get_openid', // 调用获取openid的云函数
        success: res => {
          console.log('获取到的openid:', res.result.openid);
          this.setData({ openid: res.result.openid });
          resolve(res.result.openid); // 成功获取到openid
        },
        fail: err => {
          console.error('获取openid失败', err);
          reject(err); // 获取失败时拒绝Promise
        }
      });
    });
  },

  // 调用云函数创建房间
  createRoomInCloud(openid,selectedCapacity) {
    return new Promise((resolve, reject) => {
      wx.cloud.callFunction({
        name: 'create_room', // 调用创建房间的云函数
        data: {
          ownerOpenid: openid,
          selectedCapacity: selectedCapacity // 传递选择的人数
        },
        success: res => {
          console.log('房间创建成功，roomId:', res.result.roomId);
          resolve(res.result.roomId); // 成功创建房间并返回房间号
        },
        fail: err => {
          console.error('房间创建失败', err);
          reject(err); // 创建房间失败时拒绝Promise
        }
      });
    });
  }
});
